FROM node:12.16 as builder
COPY . /app
WORKDIR /app

ENV TZ="Asia/Shanghai"
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone

RUN npm config set registry https://registry.npm.taobao.org --global && npm config set disturl https://npm.taobao.org/dist --global
RUN npm install
RUN npm run build

FROM nginx
COPY --from=builder /app/dist /app
## 加入nginx 基础配置
RUN echo 'user nginx; \
          worker_processes 1; \
          \
          error_log /app/error_nginx.log crit; \
          pid /var/run/nginx.pid; \
          worker_rlimit_nofile 51200; \
          \
          events { \
            use epoll; \
            worker_connections 51200; \
            multi_accept on; \
          } \
          \
          http { \
            include mime.types; \
            default_type application/octet-stream; \
            server_names_hash_bucket_size 128; \
            client_header_buffer_size 32k; \
            large_client_header_buffers 4 32k; \
            client_max_body_size 1024m; \
            client_body_buffer_size 10m; \
            sendfile on; \
            tcp_nopush on; \
            keepalive_timeout 120; \
            server_tokens off; \
            tcp_nodelay on; \
          \
            fastcgi_connect_timeout 300; \
            fastcgi_send_timeout 300; \
            fastcgi_read_timeout 300; \
            fastcgi_buffer_size 64k; \
            fastcgi_buffers 4 64k; \
            fastcgi_busy_buffers_size 128k; \
            fastcgi_temp_file_write_size 128k; \
            fastcgi_intercept_errors on; \
          \
            add_header Access-Control-Allow-Origin *; \
            add_header Access-Control-Allow-Credentials true; \
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie, Authorization, authorization"; \
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS,PUT,DELETE,HEAD; \
            add_header Access-Control-Expose-Headers *; \
          \
            #Gzip Compression \
            gzip on; \
            gzip_buffers 16 8k; \
            gzip_comp_level 6; \
            gzip_http_version 1.1; \
            gzip_min_length 256; \
            gzip_proxied any; \
            gzip_vary on; \
            gzip_types \
              text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml \
              text/javascript application/javascript application/x-javascript \
              text/x-json application/json application/x-web-app-manifest+json \
              text/css text/plain text/x-component \
              font/opentype application/x-font-ttf application/vnd.ms-fontobject \
              image/x-icon; \
            gzip_disable "MSIE [1-6]\.(?!.*SV1)"; \
          \
            open_file_cache max=1000 inactive=20s; \
            open_file_cache_valid 30s; \
            open_file_cache_min_uses 2; \
            open_file_cache_errors on; \
          \
            ########################## vhost ############################# \
              include vhost/*.conf; \
          }' > /etc/nginx/nginx.conf 
 
RUN mkdir /etc/nginx/vhost 
RUN echo "server { \
            listen 80; \
            index index.html index.htm index.php; \
            root /app; \
          \
            location / { \
                try_files $uri /index.html $uri/; \
            } \
          \
            location ~ .*\.(svg|ttf|eot|woff|mp3|gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { \
              expires 30d; \
              access_log off; \
            } \
            location ~ .*\.(js|css)?$ { \
              expires 7d; \
              access_log off; \
            } \
            location ~ /\.ht { \
              deny all; \
            } \
          }" > /etc/nginx/vhost/web_http.conf 
EXPOSE 80 
